package main

import (
	"context"
	"fmt"
	"github.com/bishisimo/errlog"
	"github.com/lucas-clemente/quic-go"
	"github.com/lucas-clemente/quic-go/http3"
	"net/http"
	_ "net/http/pprof"
	."quic-demo/logger"
	"quic-demo/utils"
)

func main() {
	SeverByQuic()
}

func SeverByQuic()  {
	listener, err := quic.ListenAddr("localhost:4433", utils.GetTLSConfig(), nil)
	errlog.Debug(err)
	defer listener.Close()
	for{
		Logger.Info().Msg("start accept")
		sess, err := listener.Accept(context.Background())
		errlog.Debug(err)
		Logger.Info().Msg("accept handle")
		stream, err := sess.AcceptStream(context.Background())
		errlog.Debug(err)
		Logger.Info().Msg("AcceptStream")
		go func() {
			for {
				buff:=make([]byte,10)
				n, err := stream.Read(buff)
				if err != nil {
					return
				}
				utils.Println(string(buff[:n]))
				_, err = stream.Write([]byte("hhh"))
				if err != nil {
					Logger.Err(err)
					return
				}
			}
		}()
	}
}

func ServerByHttp3()  {
	//controller:=new(Controller)
	http.Handle("/", http.FileServer(http.Dir("./")))
	errlog.Debug(http3.ListenAndServeQUIC("localhost:4433", "ssl/cert.pem", "ssl/key.pem", nil))
}

type Controller struct {
	
}

func (self Controller) ServeHTTP(res http.ResponseWriter, req *http.Request)  {
	fmt.Println("mmm")
	switch req.URL.Path {
	case "/list":
		fmt.Println("list")
	case "/price":
		fmt.Println("price")
	default:
		fmt.Println("hhh")
	}
}